//
//  TextViewController.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/17.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "TextViewController.h"
#import "TextModel.h"
#import "TextTableViewCell.h"
#import "NetWorkHandle.h"
#import "MJRefresh.h"
#define TextFont [UIFont systemFontOfSize:15]

@interface TextViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *textTableView;

/**数据数组*/
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation TextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getData];
    
    [self loadTableView];
    
    self.textTableView.allowsSelection = NO;
    
    [self refresh];
}

#pragma mark --- 加载数据
- (void)getData
{
    NSString *url = @"http://api.budejie.com/api/api_open.php?a=newlist&appname=baisishequ&asid=79C90406-DB8A-4758-9466-DEDB502C2A14&c=data&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&from=ios&jbk=0&mac=&market=&openudid=3739a3941c7bb4f82c78c8c53228edcb4a14f0d0&page=0&per=20&sub_flag=1&type=29&udid=&ver=3.6";
    
    _dataArray = [NSMutableArray array];
    
    [NetWorkHandle getDataWithURLString:url compare:^(id object) {
       
        NSDictionary *dict = object;
        
        NSArray *tempArr = dict[@"list"];
        
        for (NSDictionary *dict in tempArr) {
            
            TextModel *model = [TextModel textModelWithDict:dict];
            
            [_dataArray addObject:model];
        }
        
        [self.textTableView reloadData];
        
        NSLog(@"%--------------------ld",_dataArray.count);
    }];
    [self.textTableView.header endRefreshing];
}

#pragma mark --- 加载tableView
- (void)loadTableView
{
    self.textTableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    self.textTableView.dataSource = self;
    self.textTableView.delegate = self;
    [self.textTableView registerClass:[TextTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:_textTableView];
}

#pragma mark --- 实现tableView代理方法
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

//创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    TextModel *model = self.dataArray[indexPath.row];
    
    cell.textModel = model;
    
    return cell;
}

//每个cell的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TextModel *model = [[TextModel alloc]init];
    model = _dataArray[indexPath.row];
    return 130 + [self stringHeightWithString:model.text fontSize:15 contentSize:(CGSizeMake(self.view.frame.size.width - 20, 10000))];
}

- (CGFloat)stringHeightWithString:(NSString *)str fontSize:(CGFloat)fontSize contentSize:(CGSize)size
{
    // 第一个参数, 代表最大范围,
    // 第二个参数, 代表的是 是否考虑字体. 是否考虑字号
    // 第三个参数, 代表是使用什么字体什么字号
    // 第四个参数, 用不到,基本上都是 nil
    
    CGRect stringRect = [str boundingRectWithSize:size options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
    
    return stringRect.size.height;
}



- (CGFloat)cellHeigthForModel:(TextModel *)model
{
    CGSize size = [self sizeWithText:model.text font:TextFont maxSize:(CGSizeMake(355, MAXFLOAT))];
    return size.height + 50;
    
}

// 封装一个根据字符串\字体\最大大小,来计算大小的方法
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}


#pragma mark --- 上拉刷新，下拉下载
- (void)refresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 设置文字
    [header setTitle:@"下拉刷新" forState:(MJRefreshStateIdle)];
    [header setTitle:@"松开释放" forState:(MJRefreshStatePulling)];
    [header setTitle:@"正在刷新..." forState:(MJRefreshStateRefreshing)];
    // 字体
    header.stateLabel.font = [UIFont systemFontOfSize:15];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:15];
    header.stateLabel.textColor = [UIColor grayColor];

    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.automaticallyChangeAlpha = YES;
    
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    
    // 马上进入刷新状态
    [header beginRefreshing];
    
    // 设置header
    self.textTableView.header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    
    // 设置文字
    [footer setTitle:@"点击或者上拉加载" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在努力加载" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多了！" forState:MJRefreshStateNoMoreData];
    
    // 设置字体
    footer.stateLabel.font = [UIFont systemFontOfSize:14];
    
    // 设置颜色
    footer.stateLabel.textColor = [UIColor grayColor];
    
    // 设置footer
    self.textTableView.footer = footer;
}

- (void)loadNewData
{
    [self getData];
    //[self.textTableView.header endRefreshing];
    NSLog(@"adfafd");
}

- (void)loadMoreData
{
    NSLog(@"adfasdf");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
